<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/13
 * Time: 10:30
 */


header('Content-type:text/html; charset=utf-8');
// 开启Session
session_start();

# 接收用户的登录信息
$username = trim($_POST['username']);
$password = trim($_POST['password']);

// 处理用户登录信息
if (isset($_POST['register'])) {
    $username = trim($_POST['username']);
    $password = trim($_POST['password']);
    // 判断提交的登录信息
    if (($username == '') || ($password == '')) {
        // 若为空,视为未填写,提示错误,并3秒后返回登录界面
//        header('refresh:3; url=login.html');
        echo "用户名或密码不能为空";
        exit;
    } else {
        # 用户名或密码错误,同空的处理方式
        sqlCheck($username, $password);
    };
};

//从数据库读取用户信息
function sqlCheck($username, $password) {
    //数据库连接
    $mysqli = new mysqli("localhost", "root", "gcy888999", "blog");

    //检测连接
    if(mysqli_connect_error()) {
        die("连接失败: " . $mysqli->connect_error);
    }

    $sql = "INSERT INTO user(username, password) VALUES('". $username. "','". $password ."')";
    $result = $mysqli->query($sql);
    if($result) {
        header('refresh:3; url=login.html');
        echo "恭喜你注册成功,系统将在3秒后跳转到登录界面进行登陆!";
    }else {
        echo "注册失败！";
        exit;
    }

}
